package org.example.infrastructure.adapter.repository;


import lombok.extern.slf4j.Slf4j;
import org.example.domain.auth.adapter.repository.IAuthRepository;
import org.example.domain.auth.model.entity.UserEntity;
import org.example.infrastructure.dao.IUserDao;
import org.example.infrastructure.dao.po.UserPO;
import org.example.infrastructure.redis.IRedisService;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.Map;

import static org.example.types.common.RedisConstants.*;

/**
 * @author yinghuashen
 * @version 1.0
 * @description
 * @date 2025/3/4 11:08
 */

@Slf4j
@Repository
public class AuthRepository implements IAuthRepository {

    @Resource
    private IUserDao userDao;

    @Resource
    private IRedisService redisService;

    @Override
    public void insertUser(UserEntity userEntity) {
        UserPO userPO = UserPO.builder().
                userId(userEntity.getUserId()).
                userName(userEntity.getUserName())
                .userPhone(userEntity.getUserPhone())
                .userPassword(userEntity.getUserPassword()).build();
        userDao.insertUser(userPO);
    }

    @Override
    public UserEntity getUserPwdByPhone(String userPhone) {
        UserPO user = userDao.getUserByPhone(userPhone);
        return UserEntity.builder().
                userId(user.getUserId()).
                userName(user.getUserName())
                .userPhone(user.getUserPhone())
                .userPassword(user.getUserPassword()).build();
    }

    @Override
    public UserEntity getUserBasicInfoByPhone(String userPhone) {
        UserPO user = userDao.getUserByPhone(userPhone);
        return UserEntity.builder().
                userId(user.getUserId()).
                userName(user.getUserName())
               .userPhone(user.getUserPhone()).build();
    }

    @Override
    public void saveCode(String phone, String code) {
        // 过期时间120000ms
        redisService.setValue(LOGIN_CODE_PREFIX + phone, code, LOGIN_CODE_TTL);
    }

    @Override
    public String getCachedCode(String phone) {
        return redisService.getValue(LOGIN_CODE_PREFIX + phone);
    }

    @Override
    @Transactional
    public void saveUserInfoIntoCache(String token, Map<String, Object> userBasicInfoMap) {
        // 过期时间30Days
        redisService.setValue(LOGIN_USER_PREFIX + token, userBasicInfoMap, LOGIN_USER_TTL);
        redisService.setValue(LOGIN_TOKEN_PREFIX + userBasicInfoMap.get("userPhone"), token, LOGIN_TOKEN_TTL);
    }

    @Override
    public String getUserToken(String phone) {
        return redisService.getValue(LOGIN_TOKEN_PREFIX + phone);
    }

    @Override
    public void refreshUserJWTToken(String phone) {
//        // 过期时间30Days
//        redisService.setValue(LOGIN_TOKEN_PREFIX + phone, redisService.getValue(LOGIN_TOKEN_PREFIX + phone), LOGIN_TOKEN_TTL);
//        redisService.setValue(LOGIN_USER_PREFIX + redisService.getValue(LOGIN_TOKEN_PREFIX + phone), redisService.getValue(LOGIN_USER_PREFIX + redisService.getValue(LOGIN_TOKEN_PREFIX + phone)), LOGIN_USER_TTL);
        return;
    }

    @Override
    public Map<String, String> getUserBasicInfoByToken(String token) {
        return redisService.getValue(LOGIN_USER_PREFIX + token);
    }
}
